<?php
/**
 * Description of AttributeStringManager
 *
 * @author Magno
 */
class AttributeStringManager {

    private $attributeString;
    private $attributes;

    function __construct($attributeString) {
        $this->attributeString = $attributeString;
    }

    public function generateAttributesString(){
        $stringTemp = $this->attributeString;

        $indFinal = -1;
        $indTemp = -1;

        $resCol = Constantes::$RES_WORDS_COLUMNS;

        for ($i = 0; $i < count($resCol); $i++) {

            $indTemp = stripos($stringTemp, $resCol[$i]);

            if($indTemp === FALSE) continue;

            if($i == 0){
                $stringAux = str_replace(" ", "", $stringTemp);
                $stringAux = str_replace("\n", "", $stringAux);
                $stringAux = str_replace("\r", "", $stringAux);
                $indAux = stripos($stringAux, "PRIMARYKEY");
                
                if(strcmp($stringAux[$indAux-1], ",") == 0){                    
                    $indFinal = $indTemp;                    
                }
            }

            else if($indTemp < $indFinal){
                $indFinal = $indTemp;
            }

        }

        if($indFinal > 0)
            $stringTemp = trim(substr($stringTemp, 0,$indFinal));
        else
            $stringTemp = trim(substr($stringTemp, 0));

        if(strcmp($stringTemp[strlen($stringTemp)-1], ",") == 0){
            $stringTemp = trim(substr($stringTemp, 0, strlen($stringTemp)-2));
        }

        $atrs = explode(",", $stringTemp);

        for ($j = 0; $j < count($atrs); $j++) {
            $atrs[$j] = trim($atrs[$j]);
        }

        return $atrs;

    }

    public function getAttributesPK(){

        $attrTemp = trim(str_replace(" ", "", $this->attributeString));
        $attrsPK = array();

        if(stripos($attrTemp, "PRIMARYKEY(")){
            
            $ind = stripos($attrTemp, "PRIMARYKEY(")+strlen("PRIMARYKEY(");
            $attrTemp = substr($attrTemp, $ind);
            $indFinal = stripos($attrTemp,")");

            $attrTemp = substr($attrTemp, 0, $indFinal);
            
            if(stripos($attrTemp, ",")){
                $attrsPK = explode(",", $attrTemp);
            }else{
                $attrsPK [] = $attrTemp;
            }

        }

        return $attrsPK;

    }

    public function getAttributesFK(){

        $attrTemp = trim(str_replace(" ", "", $this->attributeString));
        $attrsFK = array();

        while(stripos($attrTemp, "FOREIGNKEY(")){

            $ind = stripos($attrTemp, "FOREIGNKEY(")+strlen("FOREIGNKEY(");
            $attrTemp = substr($attrTemp, $ind);
            $indFinal = stripos($attrTemp,")");

            $attrName = substr($attrTemp, 0, $indFinal);

            if(stripos($attrName, ",")){

                $arrayTemp = explode(",", $attrName);

                for ($x = 0; $x < count($arrayTemp); $x++){
                    $attrsFK[] = $arrayTemp[$x];                    
                }

            }else{
                $attrsFK [] = $attrName;
            }

        }

        return $attrsFK;

    }

    public function generateAllAttributeMeta(){

        $atrrsPK = $this->getAttributesPK();
        $atrrsFK = $this->getAttributesFK();            
        
        $attributesString = $this->generateAttributesString();
        $attrMeta = array();

        for ($i = 0; $i < count($attributesString); $i++) {
            $atMeta = $this->generateAttributeMeta($attributesString[$i]);
            if($this->existAttributeInArray($atMeta->getName(), $atrrsPK)){
                $atMeta->setPrimaryKey(true);
                $atMeta->setImportant(true);
            }
            if($this->existAttributeInArray($atMeta->getName(), $atrrsFK)){
                $atMeta->setForeignKey(true);
                $atMeta->setImportant(true);
            }
            $attrMeta[$atMeta->getName()] = $atMeta;
        }

        return $attrMeta;

    }

    public function generateAttributeMeta($attrString){

        $attrTemp = trim($attrString);

        $ind = stripos($attrTemp, " ");

        $name = $this->getAttributeName($attrTemp);
        $type = $this->getAttributeType($attrTemp);
        $default = $this->getAttributeValueDefault($attrTemp);
        $primaryKey = $this->isAttributePK($attrTemp);
        $notNull = $this->isAttributeNotNull($attrTemp);

        $important = $primaryKey;
        
        
        return new AttributeMeta($name, $type, $notNull, $primaryKey, false, $default, $important, false);
    }

    public function getAttributeName(&$attrString){

        $ind = stripos($attrString, " ");
        $name = substr($attrString, 0, $ind);
        $attrString = trim(substr($attrString, $ind));
        
        return $name;
    }

    public function getAttributeType(&$attrString){

        $type = "";

        for ($i = 0; $i < strlen($attrString); $i++) {
            
            if(strcmp($attrString[$i], ",") == 0 || strcmp($attrString[$i], " ") == 0)
                return $type;

            $type .= $attrString[$i];
        }

        $attrString = trim(substr($attrString, strlen($type)-1));

        return $type;
    }

    public function getAttributeValueDefault(&$attrString){

        $attrTemp = $attrString;
        $default = "";

        if(stripos($attrTemp, Constantes::$DEFAULT)){
            
            $indDef = stripos($attrTemp, Constantes::$DEFAULT);
            $attrTemp = trim(substr($attrTemp, $indDef+strlen(Constantes::$DEFAULT)));

            for ($i = 0; $i < strlen($attrTemp); $i++) {

                if(strcmp($attrTemp[$i], ",") == 0 || strcmp($attrTemp[$i], " ") == 0)
                    return $default;

                $default .= $attrTemp[$i];
            }

        }

        return $default;
    }

    public function isAttributeNotNull(&$attrString){
        $isNotNull = false;
        if(stripos($attrString, Constantes::$NOT_NULL)){
            $isNotNull = true;
            $attrString = str_replace(Constantes::$NOT_NULL, "", $attrString);
        }
        return $isNotNull;
    }

    public function isAttributePK(&$attrString){
        $isPK = false;
        if(stripos($attrString, Constantes::$PRIMARY_KEY)){
            $isPK = true;
            $attrString = str_replace(Constantes::$PRIMARY_KEY, "", $attrString);
        }
        return $isPK;
    }

    public function isAttributeFK(&$attrString){
        
    }

    public function existAttributeInArray($attrName, $array, $caseSensitive = false){
        
        for ($i = 0; $i < count($array); $i++) {

            $arrayName = $array[$i];

            if(!$caseSensitive){
                $attrName = trim( strtolower ($attrName) );
                $arrayName = trim( strtolower ($arrayName) );
            }

            if(strcmp($attrName, $arrayName) == 0)
                return true;

        }

        return false;
    }

    public static function findByName($attributes, $name){
        for ($i = 0; $i < count($attributes); $i++) {
            $attName = strtolower(trim($attributes[$i]->getName()));
            $name = strtolower(trim($name));
            if(strcmp($attName, $name) == 0) return $attributes[$i];
        }
        return null;
    }
    
}
?>
